var gulp = require('gulp'),
  connect = require('gulp-connect'),
  proxy = require('proxy-middleware'),
  url = require('url'),
  sass = require('gulp-sass'),
  compass = require('gulp-compass'),
  sourcemaps = require('gulp-sourcemaps'),
  del = require('del'),
  rename = require('gulp-rename');

gulp.task('html', function() {
  return gulp.src('./app/html/main.html')
    .pipe(connect.reload());
});

gulp.task('scripts', function() {

});

gulp.task('styles', ['clean'], function() {
  return gulp.src('app/styles/sass/**/*.scss')
    .pipe(compass({
      css: 'app/styles/css',
      sass: 'app/styles/sass',
      image: 'app/images'
    }))
    .pipe(gulp.dest('app/styles/css/'));
});

gulp.task('watch', function() {
  gulp.watch(['app/html/*.html'], ['html']);
  gulp.watch(['app/*.js'], ['scripts']);
  gulp.watch(['app/styles/sass/**/*.scss', 'app/images/icons/*.png'], ['styles']);
});

gulp.task('clean', function() {
  del(['app/styles/css/*', 'app/images/*.png'], function(err, deletedFiles) {
    console.log('Project was cleand:', deletedFiles.join(', '));
  });
});

gulp.task('proxy', function() {
  connect.server({
    root: 'app',
    port: '8080',
    livereload: true,
    middleware: function(connect, o) {
      return [(function() {
        var options = url.parse('http://120.27.45.161:8080/shhuopu');
        options.route = '/shhuopu';
        return proxy(options);
      })()];
    }
  });
});

gulp.task('default', ['clean', 'styles', 'scripts', 'html', 'proxy', 'watch']);

gulp.task('build', function() {

});
